﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.CommandUIDefinitionClasses;
using RibbonEditor._Global;
using RibbonEditor.AppCode;

namespace RibbonEditor
{
    public class CustomActions: IXmlBuildable, IChildDeletable
    {
        public List<ICustomAction> Actions;

        public CustomActions()
        {
            this.Actions = new List<ICustomAction>();
        }

        public CustomActions(XmlNode node)
            : this()
        {
            try
            {
                foreach (XmlNode actionXmlNode in node.SelectNodes("CustomAction"))
                {
                    if (actionXmlNode.NodeType == XmlNodeType.Comment)
                        return;

                    ICustomAction action = new CustomAction(actionXmlNode);
                    this.Actions.Add(action);
                }

                foreach (XmlNode actionXmlNode in node.SelectNodes("HideCustomAction"))
                {
                    if (actionXmlNode.NodeType == XmlNodeType.Comment)
                        return;

                    ICustomAction action = new HideCustomAction(actionXmlNode);
                    this.Actions.Add(action);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode ribbonDiffXmlNode)
        {
            try
            {
                XmlNode customActionsXmlNode = XmlHelper.AddNode(ribbonDiffXmlNode, "CustomActions");

                foreach (ICustomAction action in this.Actions)
                {
                    action.AddNode(customActionsXmlNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode customActionsNode = new TreeNode("CustomActions");
                customActionsNode.ContextMenuStrip = this.BuildContextMenu(customActionsNode);
                customActionsNode.Tag = this;

                foreach (ICustomAction action in this.Actions)
                {
                    action.BuildTreeViewElement(customActionsNode);
                }

                parentNode.Nodes.Add(customActionsNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private ContextMenuStrip BuildContextMenu(TreeNode relatedNode)
        {
            ContextMenuStrip cms = new ContextMenuStrip();
            cms.Tag = relatedNode;

            ToolStripMenuItem tsmi = new ToolStripMenuItem("Add Custom Action");
            tsmi.Click += new EventHandler(tsmi_Click);

            ToolStripMenuItem tsmi2 = new ToolStripMenuItem("Add Hide Action");
            tsmi2.Click += new EventHandler(tsmi2_Click);

            cms.Items.Add(tsmi);
            cms.Items.Add(tsmi2);

            ContextMenuStripHelper.AddDisplayXmlMenu(cms, true);
            
            return cms;
        }

        void tsmi2_Click(object sender, EventArgs e)
        {
            try
            {
                TreeNode selectedNode = (TreeNode)((ToolStripItem)sender).GetCurrentParent().Tag;

                HideCustomAction action = new HideCustomAction();
                action.BuildTreeViewElement(selectedNode);
                this.Actions.Add(action);

                selectedNode.Expand();
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        void tsmi_Click(object sender, EventArgs e)
        {
            try
            {
                TreeNode selectedNode = (TreeNode)((ToolStripItem)sender).GetCurrentParent().Tag;

                CommandUIDefinition cmdDef = new CommandUIDefinition();
                CustomAction action = new CustomAction();
                action.cmdUIDef = cmdDef;
                action.BuildTreeViewElement(selectedNode);
                this.Actions.Add(action);

                selectedNode.Expand();
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            if (o is ICustomAction)
            {
                this.Actions.Remove((ICustomAction)o);
            }
        }

        #endregion
    }
}
